﻿using LxCFD;
using System;
using System.Windows.Forms;
using System.Windows.Media.Media3D;

namespace TimeSolve
{
    public class ReadFileDialog
    {
        public void readFile(ref MeshVM mesh)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.InitialDirectory = "D:\\";
            openDialog.Title = "打开文件";

            //设置打开文件类型
            openDialog.Filter = "msh files (*.msh)|*.msh|All files (*.*)|*.*";
            openDialog.FilterIndex = 1;

            //监测文件是否存在
            openDialog.CheckFileExists = true;

            //文件打开后执行以下程序
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                switch (System.IO.Path.GetExtension(openDialog.FileName))
                {
                    case ".txt":
                        Console.WriteLine("未添加读取txt文件功能");
                        break;
                    case ".msh":
                        readFluentMesh(System.IO.Path.GetFullPath(openDialog.FileName));
                        //try
                        //{
                        //    string mes;
                        //    ReadMeshOfFluent read = new ReadMeshOfFluent();
                        //    FluentMesh meshDomain = read.tryReadMesh(System.IO.Path.GetFullPath(openDialog.FileName), out mes);
                        //    if (meshDomain != null)
                        //    {
                        //        mainWindowViewModel.meshData = meshDomain;
                        //    }
                        //    else
                        //    {
                        //        mainWindowViewModel.log.AddWithTime(mes);
                        //    }
                        //}
                        //catch
                        //{
                        //    mainWindowViewModel.log.AddWithTime("发生未知错误");
                        //}
                        break;
                    default:
                        break;
                }
                //    System.IO.Path.GetFullPath(openDialog.FileName);                          //绝对路径
                //    System.IO.Path.GetExtension(openDialog.FileName);                           //文件扩展名
                //    System.IO.Path.GetFileNameWithoutExtension(openDialog.FileName);//文件名没有扩展   名
                //System.IO.Path.GetFileName(openDialog.FileName);                          //得到文件
                //    System.IO.Path.GetDirectoryName(openDialog.FileName);                  //得到路径
            }

        }
        private void readFluentMesh(string filePath)
        {
            FluentMesh meshReader = new FluentMesh();
            if (meshReader.ReadFile(filePath))
            {
                meshPrepare();
            }
        }
        private void meshPrepare()
        {
            AppRes.FvMesh.PrepareMeshData();
            MainViewModel.mainViewModel.graphDataBase = new GraphDataBase(AppRes.FvMesh);
            MainViewModel.mainViewModel.mesh = new MeshVM();
            MainViewModel.mainViewModel.mesh.LoadVMFromMesh(AppRes.FvMesh);
        }

    }
}